package com.ebook.util;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class random {
    public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String letterChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String numberChar = "0123456789";
    private JdbcTemplate jdbcTemplate;
    /**
     * 返回一个定长的随机字符串(只包含大小写字母、数字)
     * 
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(allChar.charAt(random.nextInt(allChar.length())));
        }
        return sb.toString();
    }

    /**
     * 返回一个定长的随机纯字母字符串(只包含大小写字母)
     * 
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateMixString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(allChar.charAt(random.nextInt(letterChar.length())));
         //   sb.append("aaa");
        }
        
        
        return sb.toString();
    }

   
    public static String generateLetter(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(letter.charAt(random.nextInt(letter.length())));
         //   sb.append("aaa");
        }
        
        
        return sb.toString();
    }

    
    
    /**
     * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)
     * 
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateLowerString(int length) {
        return generateMixString(length).toLowerCase();
    }

    /**
     * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)
     * 
     * @param length
     *            随机字符串长度
     * @return 随机字符串
     */
    public static String generateUpperString(int length) {
        return generateMixString(length).toUpperCase();
    }

    /**
     * 生成一个定长的纯0字符串
     * 
     * @param length
     *            字符串长度
     * @return 纯0字符串
     */
    public static String generateZeroString(int length) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            sb.append('0');
        }
        return sb.toString();
        
    }

    /**
     * 根据数字生成一个定长的字符串，长度不够前面补0
     * 
     * @param num
     *            数字
     * @param fixdlenth
     *            字符串长度
     * @return 定长的字符串
     */
    public static String toFixdLengthString(long num, int fixdlenth) {
        StringBuffer sb = new StringBuffer();
        String strNum = String.valueOf(num);
        if (fixdlenth - strNum.length() >= 0) {
            sb.append(generateZeroString(fixdlenth - strNum.length()));
        } else {
            throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
                    + "的字符串发生异常！");
        }
        sb.append(strNum);
        return sb.toString();
    }

    /**
     * 根据数字生成一个定长的字符串，长度不够前面补0
     * 
     * @param num
     *            数字
     * @param fixdlenth
     *            字符串长度
     * @return 定长的字符串
     */
    public static String toFixdLengthString(int num, int fixdlenth) {
        StringBuffer sb = new StringBuffer();
        String strNum = String.valueOf(num);
        if (fixdlenth - strNum.length() >= 0) {
            sb.append(generateZeroString(fixdlenth - strNum.length()));
        } else {
            throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth
                    + "的字符串发生异常！");
        }
        sb.append(strNum);
        return sb.toString();
    }
    public static String generateChar(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(allChar.charAt(random.nextInt(letterChar.length())));
         //   sb.append("aaa");
        }
        return sb.toString();
    }
    
	public static  String  getBookidAndUserId(String ran)
	{   
		         if (ran!=null) {
		             Pattern p = Pattern.compile("[a-zA-Z]");;
		            ran.replaceAll("[a-zA-Z]", "");
		        }
	
		  return ran ;
		    
	}
	
	public static String input (String partid,String bookid)
	{
		String  code = generateMixString(10) ;
		if(partid.length()<2 && partid.length()>0)
		{  partid = generateLetter(1)+partid;
		//System.out.println(partid);
		}
		int  len = bookid.length();
		    if(len >0  && len <5)
		    {  bookid = generateLetter(4-len)+bookid;
		    }
	      code =code.substring(0, 2)+partid+code.substring(2, 4)+bookid+code.substring(4, 6);
	System.out.println("==="+code);
	   return code ;
	}
    
    public static void main(String[] args) {
       /// System.out.println(generateString(6));
    	
    	String  temp = generateMixString(12) ;
    	String bookid = "0002";
    	String order  = "12" ;
    	 String fill ;
    	int j = 1000;    	
    //	System.out.println(temp);
    //	System.out.println(generateMixString(6));     
    	// System.out.println(generateUpperString(6));
    	if(bookid.length()<6)
    	{  fill = temp.substring(7,6-bookid.length()+7);
    	   order = order+fill;	
    	}
    	 ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
    	JdbcTemplate jdbc = null ;
     // System.out.println(temp.substring(0, 2)+order+temp.substring(2, 4)+bookid+temp.substring(4, 6));
       //System.out.println(generateChar(2));
      //  System.out.println(generateUpperString(6));
         String input = (input("3","3"));
         String sql = "insert into partInfo (partnerid, bookid) values (?, ?)";
        jdbc.update(sql,4,4);
    	// System.out.println(input("2","99"));
    
    }

}